"""
Copyright 2012 Wieser Dietmar, Margit Mutschlechner.

This file is part of Multimedia Social Networking Platform.

Multimedia Social Networking Platform is free software: 
you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Multimedia Social Networking Platform is distributed
in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Multimedia Social Networking Platform.  
If not, see <http://www.gnu.org/licenses/>.

"""

import os
import time
from subprocess import call
import random
#from msnp.logger import Logger
import sys

if __name__ == '__main__':
#    path = "/home/franz/workspace/MSNP_0_4/src/"
#    path = "/home/dietmar/workspace/MSNP_0_6/src/"
    path = "/home/itie/Uni/master/masterarbeit/MSNP_0_7/src/"
    numberOfPeers = random.randint(10, 50)
#    numberOfPeers = 10
    numberOfConsoles = 1
    
    peersWithConsole = []
    if numberOfConsoles > 0:
        peersWithConsole = random.sample(range(1, numberOfPeers), numberOfConsoles - 1)
    
#    timestamp = sys.argv.pop()
#    Logger.logResult(timestamp + ": start to test program\n")
    
    pid = os.fork()
#    if False:
    if pid == 0:
        #initial peer
        print "child " + str(0)
        call = "python " + path + "Start.py 0 pw 0 " + sys.argv[1]
        if numberOfConsoles > 0:
            os.system('gnome-terminal --command "' + call + '" --title="0"')
        else:
            os.system(call)
    else:
        time.sleep(5)
            
        for i in range(1, numberOfPeers):
            call = "python " + path + "Start.py " + str(i) + " pw 0"
#            time.sleep(2)
            
            pid = os.fork()
            if pid == 0:
                print "child " + str(i)
                if i in peersWithConsole:
                    #peers with console
                    os.system('gnome-terminal --command "' + call + '" --title="' + str(i) + '"')
                else:
                    #peers without console
                    os.system(call)
                break

